const router = require("../router");
const url = require("url");

const { mongoose } = require("../mongodb");

const Schema = mongoose.Schema;

const navMapSchema = new Schema({
    Home: [{ name: String, path: String }],
    Equipment: [{ name: String, path: String }],
    Management: [{ name: String, path: String }],
    CPE: [{ name: String, path: String }],
    Hub: [{ name: String, path: String }],
    Switch: [{ name: String, path: String }],
    Robot: [{ name: String, path: String }],
  }),
  columnMapSchema = new Schema({
    Home: [
      {
        id: String,
        title: String,
        subTitle: String,
        prev: [String],
        children: [String],
      },
    ],
    Equipment: [
      {
        id: String,
        title: String,
        subTitle: String,
        prev: [String],
        children: [String],
      },
    ],
    Management: [
      {
        id: String,
        title: String,
        subTitle: String,
        prev: [String],
        children: [String],
      },
    ],
    CPE: [
      {
        id: String,
        title: String,
        subTitle: String,
        prev: [String],
        children: [String],
      },
    ],
    Hub: [
      {
        id: String,
        title: String,
        subTitle: String,
        prev: [String],
        children: [String],
      },
    ],
    Switch: [
      {
        id: String,
        title: String,
        subTitle: String,
        prev: [String],
        children: [String],
      },
    ],
    Robot: [
      {
        id: String,
        title: String,
        subTitle: String,
        prev: [String],
        children: [String],
      },
    ],
  });
const NavMap = mongoose.model("NavMap", navMapSchema),
  ColumnMap = mongoose.model("ColumnMap", columnMapSchema);

// 简单化操作，设计时可对mongodb数据库进行更细粒度的集合处理
const navMap = new NavMap({
  Home: [
    {
      name: "全部",
      path: "/home",
    },
  ],
  Equipment: [
    {
      name: "全部",
      path: "/home",
    },
    {
      name: "工业设备",
      path: "/home/equipment",
    },
  ],
  Management: [
    {
      name: "全部",
      path: "/home",
    },
    {
      name: "工业设备",
      path: "/home/equipment",
    },
    {
      name: "设备管理",
      path: "/home/equipment/management",
    },
  ],
  CPE: [
    {
      name: "全部",
      path: "/home",
    },
    {
      name: "工业设备",
      path: "/home/equipment",
    },
    {
      name: "设备管理",
      path: "/home/equipment/management",
    },
    {
      name: "CPE设备",
      path: "/home/equipment/management/cpe",
    },
  ],
  Hub: [
    {
      name: "全部",
      path: "/home",
    },
    {
      name: "工业设备",
      path: "/home/equipment",
    },
    {
      name: "设备管理",
      path: "/home/equipment/management",
    },
    {
      name: "Hub设备",
      path: "/home/equipment/management/hub",
    },
  ],
  Switch: [
    {
      name: "全部",
      path: "/home",
    },
    {
      name: "工业设备",
      path: "/home/equipment",
    },
    {
      name: "设备管理",
      path: "/home/equipment/management",
    },
    {
      name: "交换机设备",
      path: "/home/equipment/management/switch",
    },
  ],
  Robot: [
    {
      name: "全部",
      path: "/home",
    },
    {
      name: "工业设备",
      path: "/home/equipment",
    },
    {
      name: "设备管理",
      path: "/home/equipment/management",
    },
    {
      name: "机器人设备",
      path: "/home/equipment/management/robot",
    },
  ],
});

router.get("/nav", async function (req, res) {
  const { name } = url.parse(req.url, true).query;
  console.log("/nav", name);
  console.log("nav", navMap[`${name}`]);
  navMap.save().then(
    (result) => {
      console.log("成功的回调", result);
      res.json({
        code: "0",
        data: {
          nav: navMap[`${name}`],
        },
        msg: "成功",
        success: true,
      });
    },
    (err) => {
      console.log("失败的回调", err);

      res.json({
        code: "-1",
        data: {
          err: err,
        },
        msg: "失败",
        success: false,
      });
    }
  );
});

const columnMap = new ColumnMap({
  Home: [
    {
      id: "equipment",
      title: "工业设备",
      subTitle: "全部",
      prev: [],
      children: [
        "management",
        "cpe",
        "camera",
        "wifi",
        "hub",
        "usb",
        "ethernet",
        "switch",
        "two",
        "three",
        "four",
        "robot",
        "arm",
        "leg",
      ],
    },
  ],
  Equipment: [
    {
      id: "management",
      title: "设备管理",
      subTitle: "全部 - 工业设备",
      prev: ["equipment"],
      children: [
        "cpe",
        "camera",
        "wifi",
        "hub",
        "usb",
        "ethernet",
        "switch",
        "two",
        "three",
        "four",
        "robot",
        "arm",
        "leg",
      ],
    },
  ],
  Management: [
    {
      id: "cpe",
      title: "CPE设备",
      subTitle: "全部 - 工业设备 - 设备管理",
      prev: ["equipment", "management"],
      children: ["camera", "wifi"],
    },
    {
      id: "hub",
      title: "Hub设备",
      subTitle: "全部 - 工业设备 - 设备管理",
      prev: ["equipment", "management"],
      children: ["usb", "ethernet"],
    },
    {
      id: "switch",
      title: "交换机设备",
      subTitle: "全部 - 工业设备 - 设备管理",
      prev: ["equipment", "management"],
      children: ["two", "three", "four"],
    },
    {
      id: "robot",
      title: "机器人设备",
      subTitle: "全部 - 工业设备 - 设备管理",
      prev: ["equipment", "management"],
      children: ["arm", "leg"],
    },
  ],
  CPE: [
    {
      id: "camera",
      title: "摄像头",
      prev: ["equipment", "management", "cpe"],
      subTitle: "全部 - 工业设备 - 设备管理 - CPE设备",
      children: [],
    },
    {
      id: "wifi",
      title: "WiFi",
      prev: ["equipment", "management", "cpe"],
      subTitle: "全部 - 工业设备 - 设备管理 - CPE设备",
      children: [],
    },
  ],
  Hub: [
    {
      id: "usb",
      title: "USB Hub",
      prev: ["equipment", "management", "hub"],
      subTitle: "全部 - 工业设备 - 设备管理 - Hub设备",
      children: [],
    },
    {
      id: "ethernet",
      title: "Ethernet Hub",
      prev: ["equipment", "management", "hub"],
      subTitle: "全部 - 工业设备 - 设备管理 - Hub设备",
      children: [],
    },
  ],
  Switch: [
    {
      id: "two",
      title: "二层交换机",
      prev: ["equipment", "management", "switch"],
      subTitle: "全部 - 工业设备 - 设备管理 - 交换机设备",
      children: [],
    },
    {
      id: "three",
      title: "三层交换机",
      prev: ["equipment", "management", "switch"],
      subTitle: "全部 - 工业设备 - 设备管理 - 交换机设备",
      children: [],
    },
    {
      id: "four",
      title: "四层交换机",
      prev: ["equipment", "management", "switch"],
      subTitle: "全部 - 工业设备 - 设备管理 - 交换机设备",
      children: [],
    },
  ],
  Robot: [
    {
      id: "arm",
      title: "机械臂",
      prev: ["equipment", "management", "robot"],
      subTitle: "全部 - 工业设备 - 设备管理 - 机器人设备",
      children: [],
    },
    {
      id: "leg",
      title: "腿式机器人",
      prev: ["equipment", "management", "robot"],
      subTitle: "全部 - 工业设备 - 设备管理 - 机器人设备",
      children: [],
    },
  ],
});

router.get("/columns", async function (req, res) {
  const { name } = url.parse(req.url, true).query;
  console.log("/columns", name);

  columnMap.save().then(
    (result) => {
      console.log("成功的回调", result);
      res.json({
        code: "0",
        data: {
          list: columnMap[`${name}`],
        },
        msg: "成功",
        success: true,
      });
    },
    (err) => {
      console.log("失败的回调", err);

      res.json({
        code: "-1",
        data: {
          err: err,
        },
        msg: "失败",
        success: false,
      });
    }
  );
});

router.post("/search", async function (req, res) {
  const { value } = req.body;
  console.log("/columns", value);

  const names = Object.values(columnMap).flat();

  console.log("names", names);
  const list = names.filter((f) => f.title.indexOf(value) != -1);

  res.json({
    code: "0",
    data: {
      list,
    },
    msg: "成功",
    success: true,
  });
});

module.exports = router;
